// pages/parent/parent.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var id = e.id;
    this.setData({
      id:id
    });
    var that = this;
    util.request('parents/detail', {id:id}, function (data) {
      if(data.code == 1){
        that.setData({
          parent:data.data.parent,
          article:data.data.article,
        });
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  showModal:function(e){
    this.setData({
      modalName: 'modal1',
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },
  
    /**
   * 取消关注
   * @param {*} e 
   */
  nofollow:function(e){
    var that = this;
    util.request('follow/delete', {type:1,parent_id:that.data.id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/follow/follow',
          })
        },1500)
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  ViewImage(e) {
    var array = this.data.article[e.currentTarget.dataset.index].image_array,arr = [];
   
    for(var i = 0;i<array.length;i++){
      arr[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: arr,
      current: e.currentTarget.dataset.url
    });
  },

    /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.article[index],that = this;
    var collect = data.collect;
    util.request('collect/save', {related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'article['+index+'].collect';
        if(collect.is_collect > 0){
          that.setData({
              [row]:{
                count:collect.count-1,
                is_collect:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:collect.count+1,
              is_collect:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.article[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:3,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'article['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})